<?php
/**
 * ComProducts.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2021/1/17- 下午4:31
 */

namespace common\jobs\models;


use common\models\com\ComProducts as BaseComProducts;
use common\jobs\services\SyncEsService;

class ComProducts extends BaseComProducts
{


    /**
     * 关联云库商品表详细
     * @return \yii\db\ActiveQuery
     */
    public function getDetail()
    {
        return $this->hasOne(ComProductsDetail::class,['product_id' => 'id']);
    }



    /**
     * 关联云库表sku信息
     * @return \yii\db\ActiveQuery
     */
    public function getSkus()
    {
        return $this->hasMany(ComProductsSkus::class,['product_id'=>'id']);
    }

    /**
     * 数据变更后同步es
     * @param bool $insert
     * @param array $changedAttributes
     * @throws \yii\base\Exception
     */
    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        if ($this->id) {
            $countItems = \api\models\ComProducts::find()->where(['id' => $this->id])->count();
            if ($countItems) {
                $syncEsService = new SyncEsService();
                $syncEsService->syncCloudProduct([$this->id]);
            }
        }
    }
}
